 

function arrangeTasksCtrl($scope, $http, ngDialog, $state, paFetchService) {
  'ngInject';

  this.units = [];
  this.sectionList = [];
  paFetchService.getOrgList().then((resp) => {
    this.units = resp.data;
  });
  paFetchService
    .getSectionList()
    .then((resp) => {
      this.sectionList = resp.data;
    });
  // 安排任务
  this.task = {};

  this.openItemDetail = function(item) {
    $scope.item = item;
    ngDialog.open({
      template: 'itemDetailDialog',
      className: 'ngdialog-theme-default pa-special-check-item-list-theme',
      scope: $scope,
    });
  }
  this.innerCheckItemList = [];
  this.outerCheckItemList = [];

  this.treeActive = false;

  // 单个考核项内容
  $scope.itemDetail1 = {};

  // 打开tree
  this.openTree = () => {
    this.treeActive = true;
  };

  // 关闭tree
  this.closeTree = () => {
    this.treeActive = false;
  };

  // 获得扣分类型
  this.getPenaltyPointsType = (type) => {
    if (type == 1) {
      return '按分值扣分';
    } else if (type == 2) {
      return '按问题次数扣分';
    }
  };
  // 获得扣分分值
  this.getPenaltyPointsValue = (penaltyDetail) => {
    let type = penaltyDetail.kouFenType;
    let floorValue = penaltyDetail.kouFenLower;
    let ceilValue = penaltyDetail.kouFenUpper;
    if (type == 1) {
      if (floorValue == ceilValue) {
        return `${floorValue}分`;
      } else {
        return `${floorValue}至${ceilValue}分`;
      }
    } else if (type == 2) {
      return `每处${floorValue}分`;
    }
  };

  // 考核项 => viewModel
  this.handleDataToViewModel = (data) => {
    let _this = this;
    let isDefaultNodeSelected = false;

    function recursionHandleData(data) {
      data.forEach((item) => {
        item.name = item.checkCategoryName;
        item.open = true;
        itemDetail1 = item;
        if (item.sonsList) {
          item.children = item.sonsList;
          recursionHandleData(item.sonsList);
        }
      });
    }
    recursionHandleData(data);
  };
  // 根据数据渲染ztree
  this.renderZtree = () => {
    let _this = this;
    // click触发事件
    function zTreeOnCheck(event, treeId, treeNode) {
      var checked = treeNode.checked;
      var rootNode = angular.copy(treeNode);
      while (rootNode && rootNode.getParentNode()) {
        rootNode = rootNode.getParentNode();
      }
      var rootNodeName = rootNode.name;

      $scope.$apply(() => {
        if (!treeNode.isParent) {
          checked ? addItem(treeNode) : deleteItem(treeNode);
        } else {
          checked ? recursionAddItem(treeNode) : recursionDelItem(treeNode);
        }
      });

      function addItem(node) {
        if (rootNode.name == '内业') {
          _this.innerCheckItemList.push(node);
        } else if (rootNode.checkCategoryName == '外业') {
          _this.outerCheckItemList.push(node);
        }
      }

      function deleteItem(node) {
        if (rootNode.name == '内业') {
          _this.innerCheckItemList.splice(node);
        } else if (rootNode.checkCategoryName == '外业') {
          _this.outerCheckItemList.splice(node);
        }
      }

      function recursionAddItem(node) {
        node.children.forEach((item) => {
          if (item.isParent) {
            recursionAddItem(item);
          } else {
            addItem(item);
          }
        });
      }

      function recursionDelItem(node) {
        node.children.forEach((item) => {
          if (item.isParent) {
            recursionDelItem(item);
          } else {
            deleteItem(item);
          }
        });
      }
    };
    // init jquery-Ztree
    let zTreeObj;
    // zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
    let setting = {
      callback: {
        onCheck: zTreeOnCheck,
      },
      check: {
        enable: true,
        chkStyle: "checkbox",
        chkboxType: { "Y": "ps", "N": "ps" }
      },
    };
    // zTree 的数据属性，深入使用请参考 API 文档（zTreeNode 节点数据详解）
    let zNodes = this.categoryList;
    $(document).ready(function(){
      let zTreeObj = $.fn.zTree.init($("#tree"), setting, zNodes);
      /*// 激活默认选中菜单
      let defaultNode = zTreeObj.getNodeByParam('isDefaultNode', true);
      zTreeObj.selectNode(defaultNode);*/
    });
  };
  // 考核项
  this.initCheckConfigurationView = () => {
    let _this = this;
    $http.get(`${SERVER_CONTEXT}/api/safeCheck/safe/select/all`).
    then((data) => {
      if (data.statusText != 'OK') {
      } else {
        _this.categoryList = data.data;
        _this.handleDataToViewModel(_this.categoryList);
        _this.renderZtree();
      }
    });
  };
  this.initCheckConfigurationView();

  // 保存
  this.save = (param)=>{
    // 修改表单是否提交过状态
    param.$submitted = true;

    // 表单校验
    if (param.$invalid) {
      return false;
    }
    // 结束时间>起始时间
    if (param.startDate.$viewValue < param.endDate.$viewValue) {
      let reqData = this.task;
      let innerList = [], outerList = [];
      this.innerCheckItemList.forEach((item) => {
        innerList.push({checkClassifyId: item.id});
      });
      this.outerCheckItemList.forEach((item) => {
        outerList.push({checkClassifyId: item.id});
      });
      reqData.securityCheckClassifyMap = {
        inner: innerList,
        outer: outerList
      };
      $http({
        method: 'POST',
        url: `${SERVER_CONTEXT}/api/securityActivities/specialCheck/addTask`,
        data: reqData
      }).then((resp) => {
        if (resp.data.success) {
          layer.msg(resp.data.message);
          $state.go('specialCheck');
        } else {
          layer.msg(resp.data.message);
        }
      });
    } else {
      layer.alert('结束时间应大于起始时间');
      return false;
    }
    
  };
  // 重置
  this.rest = () =>{
    this.task = {};
  };

}

export default arrangeTasksCtrl;